﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.IO;
using System.Threading;

namespace FeiFeiDianYing
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            timer1.Start();
            CreatXML();
        }
        public static String ClearHtml(String str)
        {
            return (String.IsNullOrEmpty(str)) ? String.Empty : System.Text.RegularExpressions.Regex.Replace(str, @"<[^>]*>", String.Empty, System.Text.RegularExpressions.RegexOptions.IgnoreCase);
        }
        public void CreatXML()
        {

            StreamWriter sw = null;
            try
            {
                System.Text.Encoding code = System.Text.Encoding.GetEncoding("utf-8");
                string str = Creatxmlstring();
                string mbPath = ConfigurationManager.AppSettings["xmladdress"];               
                sw = new StreamWriter(mbPath, false, code);
                sw.Write(str);
                sw.Flush();
            }
            catch (Exception ex)
            {
                CollectLib.Err.writeInLog(string.Format("生成XML异常{0}", ex.Message));
            }
            finally
            {
                if (sw != null)
                    sw.Close();
            }

        }
        private string Creatxmlstring()
        {
            try
            {
                var sql = string.Format("select  * from ff_vod  order by vod_addtime desc limit 0,500");
                var dt = new CollectLib.Manage().GetDTBySql(sql);
                var xml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?><document><webSite>http://film.wudaoting.com</webSite><webMaster>804801300@qq.com</webMaster><updatePeri>60</updatePeri>";
                foreach (DataRow row in dt.Rows)
                {
                    var title = row["vod_url"].ToString().Substring(row["vod_url"].ToString().LastIndexOf("|"));

                    var href = ConfigurationManager.AppSettings["wangzhi"] + string.Format("?s=vod-play-id-{0}-sid-0-pid-{1}.html", row["vod_id"], row["vod_url"].ToString().Split(new string[] { ".rmvb" }, StringSplitOptions.None).Length - 1);
                    var tag = row["vod_name"];
                    var doc = ClearHtml(row["vod_content"].ToString()).Length > 150 ? ClearHtml(row["vod_content"].ToString()).Substring(0, 150)+"……" : ClearHtml(row["vod_content"].ToString());
                    var imglink = ConfigurationManager.AppSettings["wangzhi"] + "Uploads/" + row["vod_pic"].ToString();
                    var date = GetDateTimeBy10int(Int64.Parse(row["vod_addtime"].ToString())).ToString("yyyy-MM-dd HH:mm");//2012-07-07 15:50

                    xml = xml + "<item><op>add</op>" +
                          "<title><![CDATA[" + title + "]]></title>" +
                          "<playLink><![CDATA[" + href + "]]></playLink>" +
                           "<imageLink><![CDATA[" + imglink + "]]></imageLink>" +
                           "<tag><![CDATA[" + tag + "]]></tag>" +
                           "<comment><![CDATA[" + doc + "]]></comment>" +
                           "<pubDate><![CDATA[" + date + "]]></pubDate>" +
                       "</item>";
                }
                xml += "</document>";
                return xml;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            return "";
        }
        public DateTime GetDateTimeBy10int(Int64 mysqltime)
        {
            Int64 begtime = mysqltime * 10000000;//100毫微秒为单位,textBox1.text需要转化的int日期
            DateTime dt_1970 = new DateTime(1970, 1, 1, 8, 0, 0);
            long tricks_1970 = dt_1970.Ticks;//1970年1月1日刻度
            long time_tricks = tricks_1970 + begtime;//日志日期刻度
            DateTime dt = new DateTime(time_tricks);//转化为DateTime
            DateTime enddt = dt.Date;//获取到日期整数
            return enddt;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            Thread th = new Thread(CreatXML);
            th.Start();
        }
    }
}
